package com.xxx.config.api;

import com.xxx.util.FileUtil;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;

import java.io.File;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

/**
 * 继承的资源基础类
 *
 * @version 1.0
 * @since 1.0
 */
@Produces(MediaType.APPLICATION_JSON)
public class AbstractResource {

    protected Response ok(Object o) {
        return Response.ok(o).build();
    }

    /**
     * @return 返回封装对象，无数据返回
     */
    protected Response success() {
        return Response.ok(new ReturnResult("操作成功！")).build();
    }

    /**
     * @return 返回封装对象，无数据返回
     */
    protected Response success(String msg) {
        return Response.ok(new ReturnResult(msg)).build();
    }

    /**
     * @return 返回封装对象，无数据返回
     */
    protected Response fail() {
        return Response.ok(new ReturnResult("操作异常，请稍后再试！")).build();
    }

    /**
     * @return 返回封装对象，无数据返回
     */
    protected Response fail(String msg) {
        return Response.ok(new ReturnResult(msg)).build();
    }

    /**
     * @return 返回封装对象，无数据返回
     */
    protected Response successDel(String msg) {
        return Response.ok(new ReturnResult(msg)).build();
    }

    /**
     * @return 返回封装对象，无数据返回
     */
    protected Response successDel() {
        return Response.ok(new ReturnResult("删除成功！")).build();
    }

    /**
     * @return 返回封装对象，无数据返回
     */
    protected Response successEdit(String msg) {
        return Response.ok(new ReturnResult(msg)).build();
    }

    /**
     * @return 返回封装对象，无数据返回
     */
    protected Response successEdit() {
        return Response.ok(new ReturnResult("修改成功！")).build();
    }

    /**
     * @return 返回封装对象，无数据返回
     */
    protected Response successSave(String msg) {
        return Response.ok(new ReturnResult(msg)).build();
    }

    /**
     * @return 返回封装对象，无数据返回
     */
    protected Response successSave() {
        return Response.ok(new ReturnResult("保存成功！")).build();
    }

    /**
     * @return 返回封装对象，无数据返回
     */
    protected Response err404() {
        return Response.ok(new ReturnResult(Response.Status.NOT_FOUND.toString(), "请求资源不存在！"))
                .status(Response.Status.NOT_FOUND)
                .build();
    }

    protected Response successPreview(File file) {
        String contentType = FileUtil.getContentType(file.getName());
        return Response.ok(file)
                .header("Content-Type", contentType)
                .header("Cache-Control", "no-cache").build();
    }

    protected Response successDownload(File file) {
        String fileName = file.getName();
        fileName = URLEncoder.encode(fileName, StandardCharsets.UTF_8);
        return Response.ok(file)
                .header("Content-Disposition", "attachment;filename=" + fileName)
                .header("Cache-Control", "no-cache").build();
    }


}
